package com.gz.mylife.app;

import java.util.ArrayList;
import java.util.List;
import com.gz.mylife.R;
import com.gz.mylife.adapter.ViewPagerAdapter;
import com.gz.mylife.util.ViewUtils;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class AdvertisementActivity extends FragmentActivity{

	private ViewPager viewPage;
	private static Fragment3 mFragment3;
	private static Fragment4 mFragment4;
	private PagerAdapter mPgAdapter;
	private RadioGroup dotLayout;
	private List<Fragment> mListFragment = new ArrayList<Fragment>();
	// 图片的地址，这里可以从服务器获取
	private static final int LOOP_ORIENTATION_FORWARD = 0;
	private static final int LOOP_ORIENTATION_BACK = 1;
	private static final int LOOP_DELAYED = 2000;
	private int mLoopOrientation = LOOP_ORIENTATION_FORWARD;
	private Handler mHandler;
	private int mCurrentPageAdvert;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_advertisement);
		initView();
		viewPage.setOnPageChangeListener(new MyPagerChangeListener());

	}

	private void initView() {
		mHandler = new Handler();
		dotLayout = (RadioGroup) findViewById(R.id.advertise_point_group);
		viewPage = (ViewPager) findViewById(R.id.viewpager);
		mFragment3 = new Fragment3();
		mFragment4 = new Fragment4();
		mListFragment.add(mFragment3);
		mListFragment.add(mFragment4);
		SplashScreen.onLoadingAdvertise();//加载网络图片
		mPgAdapter = new ViewPagerAdapter(getSupportFragmentManager(),mListFragment);
		viewPage.setAdapter(mPgAdapter);
		ViewUtils.SmoothViewPagerScrollerMaker(viewPage);
		mPgAdapter.notifyDataSetChanged();

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		mHandler.postDelayed(mLoopPlaybackTask, LOOP_DELAYED);
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		mHandler.removeCallbacks(mLoopPlaybackTask);
	}
	public class MyPagerChangeListener implements OnPageChangeListener {

		public void onPageSelected(int position) {
			mCurrentPageAdvert = position;
			switch (mLoopOrientation) {
			case LOOP_ORIENTATION_FORWARD:
				if (mCurrentPageAdvert == mPgAdapter.getCount() - 1
						|| mCurrentPageAdvert > mPgAdapter.getCount()) {// 最后一页，或者是早期数据多于最新数据
					mLoopOrientation = LOOP_ORIENTATION_BACK;
				}
				break;

			case LOOP_ORIENTATION_BACK:
				if (mCurrentPageAdvert == 0) {
					mLoopOrientation = LOOP_ORIENTATION_FORWARD;
				}
				break;
			}
		}

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			((RadioButton) dotLayout.getChildAt(position)).setChecked(true);
			
		}

	}

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		SplashScreen.urllist.clear();
		return super.onKeyDown(keyCode, event);
		
	}
	
	private Runnable mLoopPlaybackTask = new Runnable() {

		@Override
		public void run() {
			if (mPgAdapter.getCount() > 0) {
				switch (mLoopOrientation) {
				case LOOP_ORIENTATION_FORWARD:
					viewPage.setCurrentItem(mCurrentPageAdvert++);
					break;
				}
				mHandler.postDelayed(mLoopPlaybackTask, LOOP_DELAYED);
			}
		}
	};


}
